package com.bang.study.aggs;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @ClassName: AggsTvsSearch
 * @author: BANG
 * @Date: 2020/01/15 13:56
 * @Version: 1.0
 * @Modified By:
 * @Desc:
 *
 * GET /tvs/sales/_search
 * {
 *    "size" : 0,
 *    "aggs":{
 *       "price":{
 *          "histogram":{
 *             "field": "price",
 *             "interval": 2000
 *          },
 *          "aggs":{
 *             "revenue": {
 *                "sum": {
 *                  "field" : "price"
 *                }
 *              }
 *          }
 *       }
 *    }
 * }
 *
 */
public class HistogramTvsSearch {

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .put("client.transport.sniff", true)
                .build();

        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)
        );

        SearchResponse searchResponse = client.prepareSearch("tvs").setTypes("sales")
                .addAggregation(AggregationBuilders.histogram("price").field("price").interval(2000d)
                                    .subAggregation(AggregationBuilders.sum("revenue").field("price"))
                                )
                .setSize(0)
                .get();


        Aggregations aggregations = searchResponse.getAggregations();
        InternalHistogram internalHistogram = aggregations.get("price");
        List<Histogram.Bucket> buckets = internalHistogram.getBuckets();
        for (Histogram.Bucket entry : buckets) {
            InternalSum internalSum = entry.getAggregations().get("revenue");
            String valueAsString = internalSum.getValueAsString();
            System.err.println(String.format("key:{%s} doc_count:{%s} color_avg_price:{%s} ",entry.getKey(),entry.getDocCount(),valueAsString));
        }

        client.close();

    }
}
